package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.crime.*;
	import b1.scenes.fight.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.media.Sound;
	
	public class BottomNavigator extends TabController
	{
		//记录当前伸缩的状态
		public var extendFlag:Boolean;
		public var extendButton:BasicIcon; 

		private var downArrowButton:BasicIcon;
		private var upArrowButton:BasicIcon;
		private static var instance:BottomNavigator;
//		public var fightFlag:Boolean;
		//物品信息
		public var tip:Sprite;
		//物品信息的属性
		private var timer:Timer;
		private var showSecond:int;
		public var mouseOverFlag:Boolean;
		
		public function BottomNavigator()
		{
			//主要图标
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorBgr");
			var NeighborIcon:Class = ResourceHelper.getInstance().getClassByName("NeighborIcon");
			var FriendIcon:Class = ResourceHelper.getInstance().getClassByName("FriendIcon");
			var PackageIcon:Class = ResourceHelper.getInstance().getClassByName("PackageIcon");
			var EquipIcon:Class = ResourceHelper.getInstance().getClassByName("Equip30");
			super(TabControllerBackground, null, 1);
			var UpArrow:Class = ResourceHelper.getInstance().getClassByName("BottomNavigatorUpArrow");
			upArrowButton = new BasicIcon(UpArrow);
			upArrowButton.x = 700;
			upArrowButton.y = 22;
			upArrowButton.scaleY = -1;
			addChild(upArrowButton);
			upArrowButton.addEventListener(MouseEvent.CLICK, onUpDownArrowButtonClick);
			extendButton = upArrowButton;
			extendFlag = true;
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
			showSecond = 0;
			mouseOverFlag = false;
			//标签页
			var tab:Tab = new Tab("邻居", 5, 1, 5, 1);
			this.addTab(NeighborIcon, tab);
			tab = new Tab("好友", 5, 1, 5, 1);
			this.addTab(FriendIcon, tab);
			tab = new Tab("包裹", 6, 1, 5, 1);
			this.addTab(PackageIcon, tab);
			tab = new Tab("装备", 6, 1, 5, 1);
			this.addTab(EquipIcon, tab);
			//标签图标的事件（扩展自己）
			for (var i:int = 0; i < tabTitles.length; i++)
			{
				this.tabTitles[i].addEventListener(MouseEvent.CLICK, onTabIconClick);
			}
		}
		
		private function onUpDownArrowButtonClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			refresh();
			switchExtendStatus();
		}
		
		public function onTabIconClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			refresh();
			extend();
		}
		
		public function extend():void
		{
			if (!extendFlag)
			{
				switchExtendStatus();
				StoryHelper.getInstance().nextStep();
			}
		}
		
		public function hide():void
		{
			if (extendFlag)
			{
				switchExtendStatus();
			}
		}
		
		//转换当前的伸缩状态
		public function switchExtendStatus():void
		{
			if (extendFlag)
			{
				this.removeTip();
				this.y += 140;
				upArrowButton.scaleY = 1;
				upArrowButton.y =  22;
				extendFlag = false;
			}
			else
			{
				this.y -= 140;
				upArrowButton.scaleY = -1;
				upArrowButton.y =  20 + upArrowButton.height - 15;
				extendFlag = true;
				//播放音效
				var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
				var sound:Sound = new SoundButton1();
				sound.play(); 
				if(SceneContainer.getInstance().step == 3)
				{
					SceneContainer.getInstance().addStep();
				}
			}
		}
		
		public function refresh():void
		{
			refreshFriends();
			refreshPackage();
		}
		
		public function refreshPackage():void
		{
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			this.tabs[2].removeAll();
			this.tabs[3].removeAll();
			//除去防具外的所有物体
			var objectItems:Array = new Array();
			//防具（衣服）
			var equipItems:Array = new Array();
			for (var objectIndex:String in player.userPackage.objectList)
			{
				var objectNum:int = player.userPackage.objectList[objectIndex];
				if (objectNum > 0)
				{
					var objectConfig:ObjectConfig = Config.objectConfigs[int(objectIndex)];
					if (objectConfig.type != 2)
					{
						objectItems.push(objectIndex);
					}
					else
					{
						equipItems.push(objectIndex);
					}
				}
			}
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var objectItem:ObjectItem;
			//防具（衣服）同一套在一起摆放
			equipItems.sort(sortEquipsOnType);
			for (var j:int = 0; j < equipItems.length; j++)
			{
				index = equipItems[j];
				objectNum = player.userPackage.objectList[index];
				objectItem = new ObjectItem(TabItemBackground, index, objectNum);
				this.tabs[3].addItem(objectItem, objectItem.width, objectItem.height);
			}
			//除去防具（衣服）以外的东西摆放
			objectItems.sort(sortObjectsOnWeightAndPrice);
			for (j = 0; j < objectItems.length; j++)
			{
				var index:int = objectItems[j];
				objectNum = player.userPackage.objectList[index];
				objectItem = new ObjectItem(TabItemBackground, index, objectNum);
				objectConfig = Config.objectConfigs[index];
				if (objectConfig.type == 3)
					this.tabs[3].addItem(objectItem, objectItem.width, objectItem.height);
				else
					this.tabs[2].addItem(objectItem, objectItem.width, objectItem.height);
			}
		}
		
		//刷新好友(taskType: 0, 主线任务放前面；1, 节日任务放前面)
		public function refreshFriends():void
		{
			for (var i:int = 0; i < 2; i++)
			{
				this.tabs[i].removeAll();
			}
			var players:Object = PlayerInfoHelper.getInstance().players;
			//加载任务虚拟邻居
			var me:PlayerInfo = players[PlayerInfoHelper.getInstance().userId];
			var festivalTaskConfig:TaskConfig = Config.festivalTaskConfigs[me.accomplishedFestivalTaskId + 1];
			var taskConfig:TaskConfig = Config.taskConfigs[me.accomplishedTaskId + 1];
			var friendItem:FriendItem;
			if (StoryHelper.getInstance().storyModeFlag && StoryHelper.getInstance().specialCase.taskType == 0)
			{
				for (i = 0; i < taskConfig.virtualPlayerIds.length; i++)
				{
					friendItem = new FriendItem(players[taskConfig.virtualPlayerIds[i]]);
					(this.tabs[0] as Tab).addItem(friendItem, 110, 110);
				}
				for (i = 0; i < festivalTaskConfig.virtualPlayerIds.length; i++)
				{
					friendItem = new FriendItem(players[festivalTaskConfig.virtualPlayerIds[i]]);
					(this.tabs[0] as Tab).addItem(friendItem, 110, 110);
				}
			}
			else
			{
				for (i = 0; i < festivalTaskConfig.virtualPlayerIds.length; i++)
				{
					friendItem = new FriendItem(players[festivalTaskConfig.virtualPlayerIds[i]]);
					(this.tabs[0] as Tab).addItem(friendItem, 110, 110);
				}
				for (i = 0; i < taskConfig.virtualPlayerIds.length; i++)
				{
					friendItem = new FriendItem(players[taskConfig.virtualPlayerIds[i]]);
					(this.tabs[0] as Tab).addItem(friendItem, 110, 110);
				}
			}
			//好友和邻居
			for (var playerId:String in players)
			{
				var player:PlayerInfo = players[playerId];
				if (player.type > 0 && player.id > 0 && player.name != "")
				{
					friendItem = new FriendItem(player);
					(this.tabs[player.type - 1] as Tab).addItem(friendItem, 110, 110);
				}
			}
			//加载邀请好友
			for (i = 0; i < 5; i++)
			{
				friendItem = new FriendItem(null);
				(this.tabs[1] as Tab).addItem(friendItem, 110, 110);
			}
		}
		
		public static function getInstance():BottomNavigator
		{
			if (!instance)
				instance = new BottomNavigator();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			refresh();
			if (!parentScene.contains(this))
			{
				this.x = -25;
				this.y = 535;
				upArrowButton.scaleY = 1;
				upArrowButton.y = 22;
				extendFlag = false;
				parentScene.addChild(this);
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
		//显示物品信息
		public function showTip(tip:Sprite):void
		{
			if (this.tip != null)
				this.removeTip()
			this.tip = tip;
			this.tip.addEventListener(MouseEvent.MOUSE_OVER, onTipOver);
			this.tip.addEventListener(MouseEvent.MOUSE_OUT, onTipOut);
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			home.addChild(tip);
			if (SceneContainer.getInstance().step == 4)
			{
				SceneContainer.getInstance().addStep();
			}
		}
		
		
		private function onTipOver(event:MouseEvent):void
		{
			this.mouseOverFlag = true;
		}
		
		private function onTipOut(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().step == 4 ||SceneContainer.getInstance().step == 5)
			{
				return;
			}
			this.mouseOverFlag = false;
		}
		
		public function removeTip():void
		{
			if (tip != null && tip.parent != null)
			{
				tip.parent.removeChild(tip);
				tip = null;
			}
		}
		
		private function onTick(event:Event):void
		{
			if (mouseOverFlag)
			{
				this.showSecond = 1;
			}
			else
			{
				if (this.showSecond > 0)
				{
					this.showSecond--;
				}
				else
				{
					removeTip();
				}
			}
		}
		
		public function setShowSecond(newShowSecond:int):void
		{
			this.showSecond = newShowSecond;
		}
	}
}